<?php

class List_CommentElement extends ContentListElementBlog
{
	protected $_createdAt;
	protected $_profileId;
	protected $_parent;
	protected $_indent;
	protected $_ccount;
	
	/**
	 * @param CommentRow
	 * @return void
	 */
	public function __construct(BlogCommentRow $row, $rowsetForInner = null)
	{
		$this->setId($row->getId());
		$this->setName($row->getProfileRowByProfileId()->getFirstName().' '.$row->getProfileRowByProfileId()->getLastName());
		
		$ts = strtotime($row->getCreatedAt());
		$date = date("d.m.Y H:i", $ts); 
		$this->setCreatedAt($date);
		
		$res = CommentService::getInstance()->prepareMessage($row->getText());
		$this->setDescription($res['desc']);
		$this->setText($res['text']);

		$avatar = '/images/no_photo120.jpg';
		$user = $row->getProfileRowByProfileId();
		if ($user->getFileAvatar()) {
			$avatar = '/upload/'.substr($user->getFileAvatar() , 0 ,2 ).'/'.$user->getFileAvatar();
			 //$avatar = ContentList::getUrlByHash($user->getFileAvatar());
		}
		$this->setPreview($avatar);
		$this->setProfileId($user->getId());
		$this->setParent($row->getParentPath());
		$ident = intval(strlen($row->getParentPath())/11);
		$ident = ($ident>0) ? ($ident-1) : (0);
		if ($ident > 10) $ident = 10;
		$this->setIndent($ident);
		
		// Set children count where parent_path like '%{$row->getId()}___________'
		$count = 0;
		if ($rowsetForInner){
			foreach ($rowsetForInner as $str){
				preg_match_all("/.{11}/", $str->getParentPath(), $arr);
				$tutu[] = $arr;
			}
			foreach ($tutu as $st){
				if (count($st[0]) > 1){
					$anull = substr('00000000000',strlen($row->getId()));
					if ($st[0][count($st[0])-2] == $anull.$row->getId()){
						$count++;
					} 
				}
			}
		}
		$this->setCcount($count);
	}
}